import { createStore } from "redux";

const defaultState: IrootState = {
  toastData: {
    show: false,
    msg: "提示",
    type: "error",
  },
};

const reducer = (state: IrootState = defaultState, action: IrootAction) => {
  switch (action.type) {
    case "SHOW_TOAST":
      const data = action.payload as IToastPayload;
      state.toastData.msg = data.msg;
      state.toastData.type = data.type;
      state.toastData.show = true;
      break;
    case "HIDE_TOAST":
      state.toastData.show = false;
      break;
  }

  state = JSON.parse(JSON.stringify(state));
  return state;
};

const store = createStore(reducer);

export default store;
